<?php
/**
 * 添加岗位
 * @author ray
 */
apf_require_controller("HrAbstract");

class Hr_Ajax_AddJobController extends HrAbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $params = $this->format_params($params);
        $status=0;
        //检测参数合法性
        $this->_check_params($params);
        $job_bll = new Bll_Hr_Job();

        //check 未被删除的同名部门
        if(!empty($params['allarr'])){
            foreach($params['allarr'] as $row){
                $check_same = $job_bll->check_same_job(array('department_id'=>$params['departID'], 'position_id'=>$row['positionID']));
                if($check_same){
                    $this->json_encode($status,Const_Tip::ERROR_JOB_EXISTS);
                }
            }
        }

        $this->pos_bll = new Bll_Hr_Position();
        //检测需要新增的岗位是否已存在,不存在的就加入
        if(!empty($params['addarr'])){
            $name_list = array();
            foreach($params['addarr'] as $row){
                //insert or update
                if(!in_array($row['name'],$name_list)){
                    $posid = $this->pos_bll->insert_position($params['function_id'],$row['name']);
                    if($posid){//新增成功能才有对应关系
                        $params['allarr'][] = array('positionID' => $posid, 'formation'=>$row['formation']);
                        $name_list[] = $row['name'];
                    }else{
                        $this->json_encode($status,Const_Tip::ERROR_JOB_EXISTS);
                    }
                }
            }
        }

        //插入职位信息
        foreach($params['allarr'] as $row){
            $indata = array(
                    'department_id'=> $params['departID'],
                    'position_id' => $row['positionID'],
                    'plan_num' => $row['formation'],
                    'is_deleted' => 1,
                    'created' => time()
            );
            //再检测一次防止页面不刷新,再点提交按钮
            $check_same = $job_bll->check_same_job(array('department_id'=>$params['departID'], 'position_id'=>$row['positionID']));
            if($check_same){
                $this->json_encode($status,Const_Tip::ERROR_JOB_EXISTS);
            }
            $pos_infos = $this->pos_bll->get_position_by_ids(array($row['positionID']));
            $indata['position_name'] = $pos_infos[$row['positionID']]['position_name'];
            $result = $job_bll->add_job($indata);
            if($result === false){
                $this->json_encode($status,Const_Tip::ERROR_JOB_DB);
            }
        }

        $status = 1;
        $this->json_encode($status,array());


    }
    private function format_params($params){
        foreach($params['positionID'] as $index => $row){
            if($row){
                $params['allarr'][] = array('positionID' => $row, 'formation'=>intval($params['formation'][$index]));
            }else{//20130217
                if(!empty($params['positionNames'][$index])){
                    $params['addarr'][] = array('name'=>$params['positionNames'][$index],'formation'=>intval($params['formation'][$index]));
                }
            }
        }
        unset($params['positionID']);
        unset($params['formation']);
        unset($params['positionNames']);
        return $params;
    }

    /**
     * 参数检测
     */
    private function _check_params($params){
        $status = 0;
        if(empty($params['departID']) || empty($params['function_id'])){
            $this->json_encode($status,Const_Tip::ERROR_PARAM);
        }else{
            if(empty($params['allarr']) && empty($params['addarr'])){
                $this->json_encode($status,Const_Tip::ERROR_PARAM);
            }
            if(!empty($params['addarr'])){
                $position_ids = array();
                foreach($params['allarr'] as $val){
                    if(empty($val['positionID'])){
                        $this->json_encode($status,Const_Tip::ERROR_PARAM);
                    }
                    elseif(in_array($val['positionID'], $position_ids)){
                        $this->json_encode($status,Const_Tip::ERROR_REPEAT_POSITION);
                    }
                    elseif($val['formation'] > 1000){
                        $this->json_encode($status,Const_Tip::ERROR_JOB_PLAN_LENGTH);
                    }else{
                        $position_ids[] = $val['positionID'];
                    }
                }
            }
            if(!empty($params['addarr'])){//对重复岗位的验证
                $name_arr = array();
                foreach($params['addarr'] as $val){
                    if(in_array($val['name'], $name_arr)){
                        $this->json_encode($status,Const_Tip::ERROR_REPEAT_POSITION);
                    }else{
                        $name_arr[] = $val['name'];
                    }
                }
            }

        }

    }
}